<template>
  <div class="app-container">
    <!-- 添加或修改设备标识任务对话框 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" :inline="true">
        <el-form-item label="透传模式" prop="transmission" v-if="control==0">
          <template>
            <div class="w200">
              <el-radio v-model="form.transmission" :label="0">关闭</el-radio>
              <el-radio v-model="form.transmission" :label="1">启用</el-radio>
            </div>
          </template>
        </el-form-item>
        <el-form-item label="数据类型" prop="dataType">
          <el-select  v-model="form.dataType" placeholder="请选择数据类型" class="three_input three_input_mragin w200">
            <el-option label="位（bit）" :value="0">位（bit）</el-option>
            <el-option label="大端 16 位无符号" :value="3">大端 16 位无符号</el-option>
            <el-option label="大端 16 位有符号" :value="4">大端 16 位有符号</el-option>
            <el-option label="大端 32 位无符号" :value="5">大端 32 位无符号</el-option>
            <el-option label="大端 32 位有符号" :value="6">大端 32 位有符号</el-option>
            <el-option label="大端 32 位浮点数" :value="7">大端 32 位浮点数</el-option>
            <el-option label="大端 64 位无符号" :value="8">大端 64 位无符号</el-option>
            <el-option label="大端 64 位有符号" :value="9">大端 64 位有符号</el-option>
            <el-option label="大端 64 位浮点数" :value="10">大端 64 位浮点数</el-option>
            <el-option label="小端 16 位无符号" :value="11">小端 16 位无符号</el-option>
            <el-option label="小端 16 位有符号" :value="12">小端 16 位有符号</el-option>
            <el-option label="小端 32 位无符号" :value="13">小端 32 位无符号</el-option>
            <el-option label="小端 32 位有符号" :value="14">小端 32 位有符号</el-option>
            <el-option label="小端 32 位浮点数" :value="15">小端 32 位浮点数</el-option>
            <el-option label="小端 64 位无符号" :value="16">小端 64 位无符号</el-option>
            <el-option label="小端 64 位有符号" :value="17">小端 64 位有符号</el-option>
            <el-option label="小端 64 位浮点数" :value="18">小端 64 位浮点数</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="功能码" prop="funcCode">
          <el-select v-model="form.funcCode" placeholder="请选择数据类型" class="three_input three_input_mragin w200">
            <el-option label="01H,05H,15H 功能码，读写线圈" :value="0">01H,05H,15H 功能码，读写线圈</el-option>
            <el-option label="02H 功能码，读输入离散量" :value="1">02H 功能码，读输入离散量</el-option>
            <el-option label="04H 功能码，读输入寄存器" :value="2">04H 功能码，读输入寄存器</el-option>
            <el-option label="03H,06H,10H 功能码，读写保存寄存器" :value="3">03H,06H,10H 功能码，读写保存寄存器</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="起始位" prop="start" v-if="form.transmission==0">
          <el-input  class="w200" v-model="form.start" placeholder="请输入起始位" />
        </el-form-item>
        <el-form-item label="从站地址" prop="slaveStationAddr">
          <el-input  class="w200" v-model="form.slaveStationAddr" placeholder="请输入从站地址" />
        </el-form-item>
        <el-form-item label="寄存器地址" prop="dataAddr">
          <el-input  class="w200" v-model="form.dataAddr" placeholder="请输入寄存器地址" />
        </el-form-item>
        <el-form-item label="采样周期" prop="period" v-if="form.transmission==0">
          <el-input  class="w200" v-model="form.period" placeholder="请输入采样周期" />
        </el-form-item>
        <el-form-item label="应答时间" prop="respTime" v-if="form.transmission==0">
          <div class="w400">
            <el-input  class="w200" v-model="form.respTime" placeholder="请输入应答时间 ms" />
          </div>
        </el-form-item>
        <el-divider content-position="center" v-if="control==0">数据上传方式</el-divider>
        <el-form-item label="定时上传" prop="timer" v-if="form.transmission==0">
          <el-switch  class="w200" style="display: block" v-model="form.timer" active-color="#13ce66"  active-text="启用" :active-value="1" inactive-color="#ff4949" inactive-text="禁用" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="上传频率" prop="times" v-if="form.transmission==0">
          <div class="w400">
            <el-input-number  class="w200" :min="0" v-model="form.times" placeholder="请输入定时上传频率" :disabled="form.timer===0"/>
          </div>
        </el-form-item>
        <el-form-item label="数值变化" prop="dataChange" v-if="form.transmission==0">
          <el-switch  class="w200" style="display: block" v-model="form.dataChange" active-color="#13ce66"  active-text="启用" :active-value="1" inactive-color="#ff4949" inactive-text="禁用" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="变化范围" prop="changeValue" v-if="form.transmission==0">
          <div class="w400">
            <el-input-number  class="w200" v-model="form.changeValue" placeholder="请输入数据变化范围" :disabled="form.dataChange===0"/>
          </div>
        </el-form-item>
        <el-form-item label="阈值触发" prop="threshold" v-if="form.transmission==0">
          <el-switch  class="w130" style="display: block" v-model="form.threshold" active-color="#13ce66"  active-text="启用" :active-value="1" inactive-color="#ff4949" inactive-text="禁用" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="表达式" prop="condition" v-if="form.transmission==0">
          <el-select v-model="form.condition" placeholder="请选择数据类型" class="three_input three_input_mragin w100" :disabled="form.threshold===0">
            <el-option label="大于" :value="1">大于</el-option>
            <el-option label="小于" :value="3">小于</el-option>
            <el-option label="区间" :value="5">区间</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="小于" prop="thrMin" v-if="form.transmission==0">
          <el-input  class="w100" v-model="form.thrMin" placeholder="请输入小于阈值" :disabled="form.threshold===0 || form.condition===1"/>
        </el-form-item>
        <el-form-item label="大于" prop="thrMax" v-if="form.transmission==0">
          <el-input  class="w100" v-model="form.thrMax" placeholder="请输入大于阈值" :disabled="form.threshold===0|| form.condition===3"/>
        </el-form-item>
        <el-form-item label="持续时间" prop="thrMax" v-if="form.transmission==0">
          <el-input  class="w100" v-model="form.delay" placeholder="请输入持续时间" :disabled="form.threshold===0"/> ms
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="handleDelete" type="danger" icon="el-icon-delete" circle></el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
  </div>
</template>

<script>
import { getDeviceSeriesIoTask, delDeviceSeriesIoTask, addDeviceSeriesIoTask, updateDeviceSeriesIoTask } from "@/api/device/deviceSeriesIoTask";

export default {
  props:{
    sensorId:null,
    deviceTypeId:null,
    control:null,
    title:null
  },
  name: "DeviceSeriesIoTask",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备标识任务表格数据
      deviceSeriesIoTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        id: null,
        sensorId: this.sensorId,
        deviceTypeId: this.deviceTypeId,
        transmission:this.control==0?0:1,
        dataType: 0,
        start: 0,
        period: 100,
        respTime: 100,
        slaveStationAddr: 0,
        funcCode: 3,
        dataAddr: 0,
        timer: 0,
        times: 60,
        dataChange: 0,
        changeValue: 0,
        threshold: 0,
        thrMin: 0,
        thrMax: 0,
        delay: 0,
        condition:1
      },
      // 表单校验
      rules: {
        seriesIoId: [
          { required: true, message: "串口标识Id不能为空", trigger: "blur" }
        ],
        deviceTypeId: [
          { required: true, message: "设备类型Id不能为空", trigger: "blur" }
        ],
        dataType: [
          { required: true, message: "数据类型不能为空", trigger: "change" }
        ],
        start: [
          { required: true, message: "起始位不能为空", trigger: "blur" }
        ],
        period: [
          { required: true, message: "采样周期不能为空", trigger: "blur" }
        ],
        respTime: [
          { required: true, message: "应答时间 ms不能为空", trigger: "blur" }
        ],
        slaveStationAddr: [
          { required: true, message: "从站地址不能为空", trigger: "blur" }
        ],
        funcCode: [
          { required: true, message: "功能码不能为空", trigger: "blur" }
        ],
        dataAddr: [
          { required: true, message: "寄存器地址不能为空", trigger: "blur" }
        ],
        timer: [
          { required: true, message: "定时上传开启不能为空", trigger: "blur" }
        ],
        times: [
          { required: true, message: "定时上传频率不能为空", trigger: "blur" }
        ],
        dataChange: [
          { required: true, message: "数值变化开启不能为空", trigger: "blur" }
        ],
        changeValue: [
          { required: true, message: "数据变化范围不能为空", trigger: "blur" }
        ],
        threshold: [
          { required: true, message: "阈值触发开启不能为空", trigger: "blur" }
        ],
        thrMin: [
          { required: true, message: "小于阈值不能为空", trigger: "blur" }
        ],
        thrMax: [
          { required: true, message: "大于阈值不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.reset()
    this.show()
  },
  methods: {
    // 取消按钮
    cancel() {
      this.$emit("cancel",null)
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sensorId: this.sensorId,
        deviceTypeId: this.deviceTypeId,
        transmission:this.control==0?0:1,
        dataType: 0,
        start: 0,
        period: 100,
        respTime: 100,
        slaveStationAddr: 0,
        funcCode: 3,
        dataAddr: 0,
        timer: 0,
        times: 60,
        dataChange: 0,
        changeValue: 0,
        threshold: 0,
        thrMin: 0,
        thrMax: 0,
        delay: 0,
        condition:1
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    show() {
      this.reset();
      getDeviceSeriesIoTask(this.sensorId).then(response => {
        if(response.data) this.form = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeviceSeriesIoTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$emit("cancel",null)
            });
          } else {
            addDeviceSeriesIoTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$emit("cancel",null)
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete() {
      const id = this.form.id
      this.$modal.confirm('是否确认清除该数据？').then(function() {
        return delDeviceSeriesIoTask(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container .w200{
  width: 220px;
  vertical-align: middle;
  &.el-switch{line-height: 1.8;}
}
.app-container .w130{
  width: 120px;
  vertical-align: middle;
  &.el-switch{line-height: 1.8;}
}

.app-container .w100{
  width: 100px;
  vertical-align: middle;
  &.el-switch{line-height: 1.8;}
}
.app-container .w400{
  width: 420px;
  vertical-align: middle;
  &.el-switch{line-height: 1.8;}
}
</style>